//
//  $Author:: chrisandsusan3587@gmail.com                  $
//    $Date:: 2012-07-21 22:16:37 -0500 (Sat, 21 Jul 2012) $
//     $Rev:: 257                                          $
//

//  CPE 790
//  Chris Davis
//  Final Project

//  multi_thread.cpp
//  file to contain function that needs to be compiled to run on multiple CPUs instead of the GPU.

//#include <stdio.h>
//#include "project.h"

#include <omp.h>

double computeMultiAverage(unsigned int * cube,
                      unsigned long Xsize,
                      unsigned long Ysize,
                      int StartingWavelength,
                      int WavelengthsToAverage)
{
	int NumberOfElementsToAverage = 0;
	int WavelengthOffset = 0;
	double averagedResult = 0.0;
	int i;

	omp_set_num_threads(4);

	NumberOfElementsToAverage = (Xsize*Ysize) * WavelengthsToAverage;
	WavelengthOffset = (Xsize*Ysize) * StartingWavelength;

	#pragma omp parallel for reduction(+:averagedResult)
	for(i = WavelengthOffset; i < NumberOfElementsToAverage + WavelengthOffset; i++)
	{
		averagedResult = averagedResult + cube[i];
	}

	averagedResult = averagedResult / NumberOfElementsToAverage;

	return averagedResult;

}